package com.logicwin.Views;

import com.logicwin.RobotTravel.Main;
import com.logicwin.RobotTravel.R;
import com.logicwin.RobotTravel.Services.Impl.ServiceManager;
import com.logicwin.RobotTravel.utils.ConfigUtils;
import com.logicwin.RobotTravel.utils.Graphics;
import com.logicwin.RobotTravel.utils.SystemParams;
import com.logicwin.RobotTravel.Position.*;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class IntroView extends View {
	private int x;
	private int y;
	private String current;
	private int color;
	private int fontSize;
	
	public IntroView(Context context) {
		super(context);
		setupData(context);
	}

	public IntroView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setupData(context);
	}
	private void setupData(Context context) {
		x = SystemParams.getWindowWidth(context);
		y = SystemParams.getWindowHeight(context) - ConfigUtils.getNewsTextSize(context) - 1;
		current = ConfigUtils.getNextNews();
		color = ConfigUtils.getNewsTextColor();
		fontSize = ConfigUtils.getNewsTextSize(context);
	}
	protected void onDraw(Canvas canvas) {
		try {
			for (int idx = 0; idx < ServiceManager.getPositoinService()
					.getPositions(); idx++) {
				Position position = ServiceManager.getPositoinService()
						.getPostion(idx);
				if (position != null){
					int posX = position.getScreenX()*Main.screenWidth/800;
					int posY = position.getScreenY()*Main.screenHeight/480;					
					Graphics.drawImage(canvas, ((BitmapDrawable) getResources()
							.getDrawable(R.drawable.dino)).getBitmap(),
							posX, posY);
				}

			}
			if (x + Graphics.getTextSize(current, fontSize, color) > 0)
			{
				Graphics.drawText(canvas, current, x, y, fontSize, color);
				x -= ConfigUtils.getTextMoveSpeed(this.getContext());
			} else
			{
				current = ConfigUtils.getNextNews();
				x = SystemParams.getWindowWidth(this.getContext());
			}				
		} catch (Exception e) {
			Log.e("MobileMenuView::onDraw", e.toString());
		}

	}
}
